package com.san.base.controller;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.san.base.domain.TaskDO;
import com.san.base.service.TaskService;
import com.san.base.vo.TaskVO;
import com.san.common.utils.Constant;
import com.san.common.utils.PageUtils;
import com.san.common.utils.Query;
import com.san.common.utils.R;

/**
 * 任务管理业务控制类
 */
@Controller
@RequestMapping("/base/task")
public class TaskController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(TaskController.class);

	/**
	 * 本功能地址前缀
	 */
	private static final String PREFIX = "base/task";

	@Autowired
	private TaskService taskService;

	@GetMapping("")
	String taskScheduleJob() {
		return PREFIX + "/view";
	}

	@ResponseBody
	@GetMapping("/list")
	public PageUtils list(@RequestParam Map<String, Object> params) {
		// 查询列表数据
		Query query = new Query(params);
		List<TaskDO> taskScheduleJobList = taskService.list(query);
		int total = taskService.count(query);
		PageUtils pageUtils = new PageUtils(taskScheduleJobList, total);
		return pageUtils;
	}

	@GetMapping("/add")
	String add() {
		return PREFIX + "/add";
	}

	@GetMapping("/edit/{id}")
	String edit(@PathVariable("id") Long id, Model model) {
		TaskDO taskDO = null;
		try {
			taskDO = (TaskDO) taskService.getById(id);
		} catch (Exception e) {
			logger.error("获取任务信息失败",e);
		}
		model.addAttribute("taskDO", taskDO);
		return PREFIX + "/edit";
	}

	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	public R info(@PathVariable("id") Long id) {
		TaskDO taskDO = null;
		try {
			taskDO = (TaskDO) taskService.getById(id);
		} catch (Exception e) {
			logger.error("获取任务信息失败",e);
		}
		return R.ok().put("taskScheduleJob", taskDO);
	}

	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	public R save(TaskVO taskVO) {
		try {
			taskVO.setStatus(Constant.STATUS_INVALID);
			if (taskService.save(taskVO) > 0) {
				return R.ok();
			}
		} catch (Exception e) {
			logger.error("保存任务失败",e);
		}
		return R.error();
	}

	/**
	 * 修改
	 */
	@ResponseBody
	@PostMapping("/doEdit")
	public R doEdit(TaskVO taskVO) {
		try {
			taskService.doEdit(taskVO);
		} catch (Exception e) {
			logger.error("更新任务失败",e);
		}
		return R.ok();
	}

	/**
	 * 删除
	 */
	@PostMapping("/remove")
	@ResponseBody
	public R remove(Long id) {
		try {
			if (taskService.remove(id) > 0) {
				return R.ok();
			}
		} catch (Exception e) {
			logger.error("删除任务失败",e);
		}
		return R.error();
	}

	/**
	 * 删除
	 */
	@PostMapping("/batchRemove")
	@ResponseBody
	public R remove(@RequestParam("ids[]") Long[] ids) {
		try {
			taskService.batchRemove(ids);
		} catch (Exception e) {
			logger.error("批量删除任务失败",e);
		}

		return R.ok();
	}

	@PostMapping(value = "/changeJobStatus")
	@ResponseBody
	public R changeJobStatus(Long id, String cmd) {
		String label = "停止";
		if ("start".equals(cmd)) {
			label = "启动";
		} else {
			label = "停止";
		}
		try {
			taskService.changeStatus(id, cmd);
			return R.ok("任务" + label + "成功");
		} catch (Exception e) {
			logger.error("更新任务状态失败",e);
		}
		return R.ok("任务" + label + "失败");
	}

}
